<?php
namespace Application\Component;
use Doctrine\ORM\EntityManager;
use Application\Entity\User;
use DateTime;
use Application\Component\AbstractComponent;
/**
 * UserServcie
 *
 */
class UserComponent extends AbstractComponent
{
    protected  $entity="Application\\Entity\\User";

 
	function insert($user)
	{
		$datetime = new DateTime("now");
		$user->createdAt = $datetime;
		$user->modifiedAt = $datetime;
		$this->em->persist($user);
		$this->em->flush();
	}
	
	
	function update($user)
	{
		$datetime = new DateTime("now");
		$user->modifiedAt = $datetime;
		$this->em->persist($user);
		$this->em->flush();
	}
	
	function get($uid)
	{
		$user = $this->em->find($this->entity, $uid);
		return  $user;
	}
	
	function getAll()
	{
		$user = $this->em->getRepository($this->entity)->findAll();
		return  $user;
	}
	
	function getByUid($uid)
	{
		$user = $this->em->getRepository($this->entity)->getByUid($uid);
		return  $user;
	}
	
	function getUniqueByUid($uid)
	{
		$userFacebook = $this->em->getRepository($this->entity)->getUniqueByUid($uid);
		return  $userFacebook;
	}
	
	function getByUsername($username)
	{
		$user = $this->em->getRepository($this->entity)->getByUsername($username);
		return  $user;
	}
	
	function getByEmail($email)
	{
		$user = $this->em->getRepository($this->entity)->getByEmail($email);
		return  $user;
	}
	
	function getUniqueByEmail($email)
	{
		$user = $this->em->getRepository($this->entity)->getUniqueByEmail($email);
		return  $user;
	}
	
	function getByEmailAndPassword($email,$password){
		return $this->em->getRepository($this->entity)->getByEmailAndPassword($email,$password);
	}
	
	function getByKeyPassword($key)
	{
		$user = $this->em->getRepository($this->entity)->getByKeyPassword($key);
		return  $user;
	}
	
	function getByKeyConfirm($key)
	{
		$user = $this->em->getRepository($this->entity)->getByKeyConfirm($key);
		return  $user;
	}
	
	function getByEmailKeyConfirm($email,$key)
	{
		$user = $this->em->getRepository($this->entity)->getByEmailKeyConfirm($email,$key);
		return  $user;
	}
	
	function getByCountryId($countryId)
	{
		$user = $this->em->getRepository($this->entity)->getByCountryId($countryId);
		return  $user;
	}
	
	function getByUserFacebookUID($userFacebookUID)
	{
		$user = $this->em->getRepository($this->entity)->getByUserFacebookUID($userFacebookUID);
		return  $user;
	}
	
	function getByUserFacebookUIDEmail($userFacebookUID,$email)
	{
		$user = $this->em->getRepository($this->entity)->getByUserFacebookUIDEmail($userFacebookUID,$email);
		return  $user;
	}
	
	function delete($id)
	{
		$user = $this->em->find($this->entity, $id);
		$this->em->remove($user);
		$this->em->flush();
	}
	public function getListUser($arrayUserId)
	{
	    $mapUsers = array();
	    $users = $this->em->getRepository($this->entity)->getListUser($arrayUserId);
	    foreach ($users as $user)
	    {
	        $mapUsers[$user->uid] = $user;
	    }
	    return $mapUsers;
	}
}
